package com.zoe.onelink.dubbo.mvc;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.HandlerMethodReturnValueHandler;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter;

import java.util.ArrayList;
import java.util.List;


@Configuration
public class ServiceRequestMappingConfiguration {

    @Autowired
    private RequestMappingHandlerAdapter requestMappingHandlerAdapter;

    @Bean
    public ServiceHandlerMapping serviceHandlerMapping() {
        return new ServiceHandlerMapping();
    }

    @Bean
    public ServiceMethodArgumentResolver serviceMethodArgumentResolver() {
        ServiceMethodArgumentResolver serviceMethodArgumentResolver = new ServiceMethodArgumentResolver();

        // 将此参数解析器的优先级提到最高
        List<HandlerMethodArgumentResolver> argumentResolvers = new ArrayList<>();
        argumentResolvers.add(serviceMethodArgumentResolver);
        argumentResolvers.addAll(requestMappingHandlerAdapter.getArgumentResolvers());
        requestMappingHandlerAdapter.setArgumentResolvers(argumentResolvers);
        return serviceMethodArgumentResolver;
    }

    @Bean
    public ServiceMethodReturnValueHandler serviceMethodReturnValueHandler() {
        ServiceMethodReturnValueHandler serviceMethodReturnValueHandler = new ServiceMethodReturnValueHandler();

        // 将此结果包装器的优先级提到最高
        List<HandlerMethodReturnValueHandler> returnValueHandlers = new ArrayList<>();
        returnValueHandlers.add(serviceMethodReturnValueHandler);
        returnValueHandlers.addAll(requestMappingHandlerAdapter.getReturnValueHandlers());
        requestMappingHandlerAdapter.setReturnValueHandlers(returnValueHandlers);
        return serviceMethodReturnValueHandler;
    }

    @Bean
    public SwaggerExpandAspect swaggerExpandAspect() {
        return new SwaggerExpandAspect();
    }
}
